<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button class="get">get请求</button>
    <button class="post">post请求</button>
</body>
<script type="module">

    import { request } from "../js/ajax.js";

    var getBtn = document.getElementsByClassName("get")[0];
    var postBtn = document.getElementsByClassName("post")[0];


    getBtn.onclick = function () {
        request({
            type: "get",
            url: "/req",
            data: {
                // user: "a123123",
                // pwd: 123123,
            },
        }).then(data => {
            console.log(data);
        }).catch(err => {
            console.log(err);
        })
    }

    postBtn.onclick = function () {
        var xhr = new XMLHttpRequest();

        xhr.open("post", "/req", true)

        // 传输 FormData数据
        // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        // xhr.send("user=a12123&pwd=123123");

        // 传输json字符串 
        xhr.setRequestHeader("Content-Type", "application/json");
        var data = { user: "a123123", pwd: "123123" }
        xhr.send(JSON.stringify(data));

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(xhr.responseText);
            }
        }

    }







</script>

</html>